package request.login;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.abacus.client.ui.view.View;
import org.abacus.model.sys.User;
import org.abacus.platform.core.service.ServiceReturn;
import org.abacus.service.ServiceFactory;
import org.abacus.service.sys.UserService;
import org.eclipse.swt.widgets.Scrollable;
import org.eclipse.swt.widgets.Text;

import request.AbstractActionRequest;
import request.ActionRequest;
import request.ActionReturn;

public class LoginRequest extends AbstractActionRequest {

	public LoginRequest() {
		super();
		this.hasNext = true;
	}

	@Override
	public <T> ActionReturn<T> done(Set<Entry<String, Scrollable>> arg0, Class<T> arg1) {
		Map<String, String> map = new HashMap<String, String>();
		for (Entry<String, Scrollable> e : arg0) {
			if (e.getValue() instanceof Text) {
				map.put(e.getKey(), ((Text) e.getValue()).getText());
			} else {
			}
		}
		Map<String, Object> userMap = new HashMap<String, Object>();
		userMap.put("user", map);
		UserService userService = ServiceFactory.getInstance(UserService.class);
		User entity = new User();
		entity.setEmail(map.get("email"));
		entity.setPassword(map.get("password"));

		ServiceReturn<User> serviceReturn = userService.login(entity);

		// T result = this.done(user, "/user/login", arg1);
		T result = (T) Boolean.TRUE;
		ActionReturn<T> ar = new ActionReturn<T>();
		ar.setResult(result);
		return ar;
	}

	@Override
	public ActionRequest next() {
		return new RoleRequest();
	}

	@Override
	public void done() {

	}

	@Override
	public void done(View view) {
		// ActionReturn<Boolean> done = if (null != done && null !=
		// done.getResult() && done.getResult().booleanValue()) {
		// if (getActionRequest().hasNext()) {
		// getActionRequest().next().done();
		// }
		// dispose();
	}
}
